<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    let submitData = {
        "id": null,
        "companyId": null,
        "style": 1,
        "inoutNo": "IN-20220607-164471",
        "itemTypeList": "1,2",
        "inoutDate": 1654601997000,
        "storehouseId": "121",
        "summary": null,
        "inoutType": 0,
        "bz": "规范化",
        "createdTime": null,
        "createdBy": null,
        "updatedBy": null,
        "updatedTime": null,
        "deleted": null,
        "backUp": null,
        "peOrderProductVOList": [
            {
                "itemId": 3582,
                "skuId": 4851,
                "decimalNum": 10,
                "num": 0,
                "inoutPrice": 100,
                "sumPrice": 1,
                "itemTitle": "练气丹",
                "minUnitName": "个",
                "soldPrice": 100,
                "propertiesName": "默认单规格:白色",
                "skuCode": "P12345681994001",
                "itemType": "3",
                "id": 5293,
                "itemCode": "P12345681994",
                "batchNumber": null
            },
            {
                "itemId": 3582,
                "skuId": 4851,
                "decimalNum": 12,
                "num": 0,
                "inoutPrice": 100,
                "sumPrice": 1,
                "itemTitle": "练气丹",
                "minUnitName": "个",
                "soldPrice": 100,
                "propertiesName": "默认单规格:白色",
                "skuCode": "P12345681994001",
                "itemType": "3",
                "id": 5295,
                "itemCode": "P12345681994",
                "batchNumber": null
            },
            {
                "itemId": 3582,
                "skuId": 4851,
                "decimalNum": 5,
                "num": 0,
                "inoutPrice": 100,
                "sumPrice": 1,
                "itemTitle": "练气丹",
                "minUnitName": "个",
                "soldPrice": 100,
                "propertiesName": "默认单规格:白色",
                "skuCode": "P12345681994001",
                "itemType": "3",
                "id": 5297,
                "itemCode": "P12345681994",
                "batchNumber": null
            },
            {
                "itemId": 3582,
                "skuId": 4851,
                "decimalNum": 8,
                "num": 0,
                "inoutPrice": 100,
                "sumPrice": 1,
                "itemTitle": "练气丹",
                "minUnitName": "个",
                "soldPrice": 100,
                "propertiesName": "默认单规格:白色",
                "skuCode": "P12345681994001",
                "itemType": "3",
                "id": 5299,
                "itemCode": "P12345681994",
                "batchNumber": null
            },
            {
                "itemId": 3586,
                "skuId": 4854,
                "decimalNum": 10,
                "num": 0,
                "inoutPrice": 100,
                "sumPrice": 1,
                "itemTitle": "炼体丹",
                "minUnitName": "个",
                "soldPrice": 100,
                "propertiesName": "默认单规格:白色",
                "skuCode": "P12345682003001",
                "itemType": "3",
                "id": 5292,
                "itemCode": "P12345682003",
                "batchNumber": null
            },
            {
                "itemId": 3586,
                "skuId": 4854,
                "decimalNum": 12,
                "num": 0,
                "inoutPrice": 100,
                "sumPrice": 1,
                "itemTitle": "炼体丹",
                "minUnitName": "个",
                "soldPrice": 100,
                "propertiesName": "默认单规格:白色",
                "skuCode": "P12345682003001",
                "itemType": "3",
                "id": 5294,
                "itemCode": "P12345682003",
                "batchNumber": null
            },
            {
                "itemId": 3586,
                "skuId": 4854,
                "decimalNum": 5,
                "num": 0,
                "inoutPrice": 100,
                "sumPrice": 1,
                "itemTitle": "炼体丹",
                "minUnitName": "个",
                "soldPrice": 100,
                "propertiesName": "默认单规格:白色",
                "skuCode": "P12345682003001",
                "itemType": "3",
                "id": 5296,
                "itemCode": "P12345682003",
                "batchNumber": null
            },
            {
                "itemId": 3586,
                "skuId": 4854,
                "decimalNum": 8,
                "num": 0,
                "inoutPrice": 100,
                "sumPrice": 1,
                "itemTitle": "炼体丹",
                "minUnitName": "个",
                "soldPrice": 100,
                "propertiesName": "默认单规格:白色",
                "skuCode": "P12345682003001",
                "itemType": "3",
                "id": 5298,
                "itemCode": "P12345682003",
                "batchNumber": null
            }
        ]
    }
    let truthSubmitData = {
        "id": null,
        "companyId": null,
        "style": 1,
        "inoutNo": "IN-20220609-342966",
        "itemTypeList": "1,2",
        "inoutDate": 1654759541000,
        "storehouseId": "308",
        "summary": null,
        "inoutType": 0,
        "bz": null,
        "createdTime": null,
        "createdBy": null,
        "updatedBy": null,
        "updatedTime": null,
        "deleted": null,
        "backUp": null,
        "peOrderProductVOList": [
            {
                "itemId": 2929,
                "skuId": 3409,
                "decimalNum": 5,
                "num": 0,
                "inoutPrice": 2,
                "sumPrice": 10,
                "itemTitle": "组合物料",
                "minUnitName": "个",
                "soldPrice": 200,
                "propertiesName": "",
                "skuCode": "P12345688521001",
                "itemType": "3",
                "id": 5321,
                "itemCode": "P12345688521",
                "batchNumber": null,
                "childVal": false
            },
            {
                "itemId": 2929,
                "skuId": 3409,
                "decimalNum": 7,
                "num": 0,
                "inoutPrice": 2,
                "sumPrice": 14,
                "itemTitle": "组合物料",
                "minUnitName": "个",
                "soldPrice": 200,
                "propertiesName": "",
                "skuCode": "P12345688521001",
                "itemType": "3",
                "id": 5325,
                "itemCode": "P12345688521",
                "batchNumber": null,
                "childVal": false
            },
            {
                "itemId": 2929,
                "skuId": 3403,
                "decimalNum": 12,
                "num": 0,
                "inoutPrice": 0.33,
                "sumPrice": 4,
                "itemTitle": "物料A",
                "minUnitName": "个",
                "soldPrice": 0.33,
                "propertiesName": "默认单规格:大黑红",
                "skuCode": "P12345688515001",
                "itemType": "3",
                "id": null,
                "itemCode": "P12345688515001",
                "batchNumber": null,
                "childVal": true
            },
            {
                "itemId": 2929,
                "skuId": 3404,
                "decimalNum": 24,
                "num": 0,
                "inoutPrice": 0.33,
                "sumPrice": 8,
                "itemTitle": "物料B",
                "minUnitName": "个",
                "soldPrice": 0.33,
                "propertiesName": "默认单规格:小黑白",
                "skuCode": "P12345688516001",
                "itemType": "3",
                "id": null,
                "itemCode": "P12345688516001",
                "batchNumber": null,
                "childVal": true
            },
            {
                "itemId": 2929,
                "skuId": 3405,
                "decimalNum": 36,
                "num": 0,
                "inoutPrice": 0.33,
                "sumPrice": 12,
                "itemTitle": "物料C",
                "minUnitName": "件",
                "soldPrice": 0.33,
                "propertiesName": "默认单规格:大黑红",
                "skuCode": "P12345688517001",
                "itemType": "3",
                "id": null,
                "itemCode": "P12345688517001",
                "batchNumber": null,
                "childVal": true
            }
        ]
    }


    let choiceListData = [
        {
            "orderNo": 220608113827427900,
            "qualifiedQuantity": 12,
            "itemTitle": "组合物料",
            "peOrderProductVOs": [
                {
                    "id": 5321,
                    "companyId": 1553,
                    "orderProductId": 5321,
                    "proxyedCompanyId": null,
                    "relationId": 1092,
                    "orderNo": 220608113827427900,
                    "awbNo": "BD-Y16867362220608113923224133",
                    "storeOutOrderId": null,
                    "productCode": "P12345688521",
                    "productId": 2929,
                    "skuCode": "P12345688521001",
                    "skuId": 3409,
                    "imageId": "",
                    "imageUrl": null,
                    "productName": "组合物料",
                    "productSkuName": "",
                    "amount": 5,
                    "unit": "个",
                    "unitPrice": 200,
                    "classifyName": null,
                    "discount": 0,
                    "storeId": 0,
                    "status": "6",
                    "checkProfileIds": "",
                    "checkType": 1,
                    "passPercent": 0,
                    "checkPercent": 0,
                    "sendDate": null,
                    "arrivalTime": 1654659603000,
                    "deliveryDate": 1654659563000,
                    "deliveryTotal": null,
                    "decimalDeliveryTotal": 5,
                    "actualQuantityDelivered": 5,
                    "decimalStockPendingTotal": 0,
                    "inoutAmount": 0,
                    "actualQuantityDeliveredFlag": "0",
                    "createdBy": 43004,
                    "createdTime": 1654659563000,
                    "updatedBy": null,
                    "updatedTime": null,
                    "deleted": 0,
                    "companyPurchaseWaitStorehouseId": null,
                    "profileId": null,
                    "itemId": null,
                    "itemCode": "P12345688521",
                    "itemName": "组合物料",
                    "skuImage": null,
                    "propertiesName": "",
                    "unitName": "个",
                    "waitStorehouseNum": null,
                    "inStorehouseNum": null,
                    "state": null,
                    "providerSkuId": null,
                    "providerItemId": null,
                    "orderNoStr": "220608113827427918",
                    "predictArrivalTime": null,
                    "storeName": null,
                    "logisticsNo": "",
                    "predictArrivalTimeSet": null,
                    "qualifiedQuantity": null,
                    "unqualifiedQuantity": null,
                    "qualifiedQuantityEditable": null,
                    "unqualifiedQuantityEditable": null,
                    "actualDeliveredQuantity": null,
                    "acceptableQuantity": null,
                    "remark": null,
                    "decimalNum": 5,
                    "inoutPrice": 200,
                    "sumPrice": null,
                    "processCost": 200,
                    "costPrice": 0,
                    "otherExpenses": 0,
                    "batchNumber": null,
                    "itemTitle": null,
                    "minUnitName": null,
                    "checkerName": null,
                    "checkerTime": null,
                    "checkerId": null,
                    "totalPrice": null,
                    "lastRequiredTime": null,
                    "pePurchaseOrderGoodId": null,
                    "supplierType": null,
                    "withinCode": "2646436",
                    "receiveAddress": null,
                    "pePurchaseOrderType": null,
                    "itemType": null,
                    "childVal": null,
                    "forAcceptanceAmount": null
                },
                {
                    "id": 5325,
                    "companyId": 1553,
                    "orderProductId": 5325,
                    "proxyedCompanyId": null,
                    "relationId": 1092,
                    "orderNo": 220608113827427900,
                    "awbNo": "BD-Y16867362220608113951435054",
                    "storeOutOrderId": null,
                    "productCode": "P12345688521",
                    "productId": 2929,
                    "skuCode": "P12345688521001",
                    "skuId": 3409,
                    "imageId": "",
                    "imageUrl": null,
                    "productName": "组合物料",
                    "productSkuName": "",
                    "amount": 7,
                    "unit": "个",
                    "unitPrice": 200,
                    "classifyName": null,
                    "discount": 0,
                    "storeId": 0,
                    "status": "6",
                    "checkProfileIds": "",
                    "checkType": 1,
                    "passPercent": 0,
                    "checkPercent": 0,
                    "sendDate": null,
                    "arrivalTime": 1654659606000,
                    "deliveryDate": 1654659591000,
                    "deliveryTotal": null,
                    "decimalDeliveryTotal": 7,
                    "actualQuantityDelivered": 7,
                    "decimalStockPendingTotal": 0,
                    "inoutAmount": 0,
                    "actualQuantityDeliveredFlag": "0",
                    "createdBy": 43004,
                    "createdTime": 1654659591000,
                    "updatedBy": null,
                    "updatedTime": null,
                    "deleted": 0,
                    "companyPurchaseWaitStorehouseId": null,
                    "profileId": null,
                    "itemId": null,
                    "itemCode": "P12345688521",
                    "itemName": "组合物料",
                    "skuImage": null,
                    "propertiesName": "",
                    "unitName": "个",
                    "waitStorehouseNum": null,
                    "inStorehouseNum": null,
                    "state": null,
                    "providerSkuId": null,
                    "providerItemId": null,
                    "orderNoStr": "220608113827427918",
                    "predictArrivalTime": null,
                    "storeName": null,
                    "logisticsNo": "123456",
                    "predictArrivalTimeSet": null,
                    "qualifiedQuantity": null,
                    "unqualifiedQuantity": null,
                    "qualifiedQuantityEditable": null,
                    "unqualifiedQuantityEditable": null,
                    "actualDeliveredQuantity": null,
                    "acceptableQuantity": null,
                    "remark": null,
                    "decimalNum": 7,
                    "inoutPrice": 200,
                    "sumPrice": null,
                    "processCost": 200,
                    "costPrice": 0,
                    "otherExpenses": 0,
                    "batchNumber": null,
                    "itemTitle": null,
                    "minUnitName": null,
                    "checkerName": null,
                    "checkerTime": null,
                    "checkerId": null,
                    "totalPrice": null,
                    "lastRequiredTime": null,
                    "pePurchaseOrderGoodId": null,
                    "supplierType": null,
                    "withinCode": "2646436",
                    "receiveAddress": null,
                    "pePurchaseOrderType": null,
                    "itemType": null,
                    "childVal": null,
                    "forAcceptanceAmount": null
                }
            ],
            "itemGroupInfoList": [
                {
                    "id": null,
                    "companyId": null,
                    "itemId": 2929,
                    "itemChildId": 3403,
                    "itemChildNum": 1,
                    "createdBy": null,
                    "createdTime": null,
                    "withinCode": "346262301",
                    "minUnitName": "个",
                    "propertiesName": "默认单规格:大黑红",
                    "itemCategoryName": null,
                    "costPrice": null,
                    "itemChildName": "物料A",
                    "itemChildCode": "P12345688515001",
                    "itemType": null,
                    "inoutPriceDec": 0.3333333333333333,
                    "outPrice": null,
                    "batchNumber": null,
                    "parentSkuId": null,
                    "snsNumber": null,
                    "decimalInventory": null,
                    "childInventoryNumber": 12,
                    "childTotalPrice": 4
                },
                {
                    "id": null,
                    "companyId": null,
                    "itemId": 2929,
                    "itemChildId": 3404,
                    "itemChildNum": 2,
                    "createdBy": null,
                    "createdTime": null,
                    "withinCode": "346262302",
                    "minUnitName": "个",
                    "propertiesName": "默认单规格:小黑白",
                    "itemCategoryName": null,
                    "costPrice": null,
                    "itemChildName": "物料B",
                    "itemChildCode": "P12345688516001",
                    "itemType": null,
                    "inoutPriceDec": 0.3333333333333333,
                    "outPrice": null,
                    "batchNumber": null,
                    "parentSkuId": null,
                    "snsNumber": null,
                    "decimalInventory": null,
                    "childInventoryNumber": 24,
                    "childTotalPrice": 8
                },
                {
                    "id": null,
                    "companyId": null,
                    "itemId": 2929,
                    "itemChildId": 3405,
                    "itemChildNum": 3,
                    "createdBy": null,
                    "createdTime": null,
                    "withinCode": "346262303",
                    "minUnitName": "件",
                    "propertiesName": "默认单规格:大黑红",
                    "itemCategoryName": null,
                    "costPrice": null,
                    "itemChildName": "物料C",
                    "itemChildCode": "P12345688517001",
                    "itemType": null,
                    "inoutPriceDec": 0.3333333333333333,
                    "outPrice": null,
                    "batchNumber": null,
                    "parentSkuId": null,
                    "snsNumber": null,
                    "decimalInventory": null,
                    "childInventoryNumber": 36,
                    "childTotalPrice": 12
                }
            ],
            "propertiesName": "",
            "minUnitName": "个",
            "inoutPrice": 2,
            "imageUrl": null,
            "soldPrice": 200,
            "purchaseQuantity": 50,
            "unqualifiedQuantity": 0,
            "skuCode": "P12345688521001",
            "mainMaterialInventoryNum": 12,
            "mainMaterialTotalPrice": 24
        }
    ]

    // 验证所有子物料的总价是否等于主物料的总价
    function checkPriceIsEqual() {
        choiceListData.find(item => {
            // 多条物流单号的主物料总数量
            const mainMaterialInventoryNum = item.peOrderProductVOs.map(o => o.amount).reduce((a, b) => (a + b))
            // 主物料的总价
            const mainMaterialTotalPrice = (item.inoutPrice || 0) * mainMaterialInventoryNum
            // 子物料的总价
            const childMaterialTotalPrice = item.itemGroupInfoList.map(item2 => item2.childTotalPrice).reduce((a, b) => (a + b))

            console.log(childMaterialTotalPrice, mainMaterialTotalPrice);
            if (childMaterialTotalPrice !== mainMaterialTotalPrice) {
                return console.error('子物料总价和主物料总价不一样，请重新设置')
            }
        })
    }
    checkPriceIsEqual()

    const mainChildMaterialList = []
    choiceListData.forEach(item => {
        if (item.peOrderProductVOs?.length > 0) {
            mainChildMaterialList.push(...item.peOrderProductVOs.map(item2 => ({
                ...{
                    itemId: item2.productId,
                    skuId: item2.skuId, //1skuid,
                    decimalNum: item2.decimalNum, //1
                    num: 0, //数量,
                    inoutPrice: item.inoutPrice, //价格,
                    sumPrice: (item.inoutPrice || 0) * (item2.decimalNum || 0), //总价
                    itemTitle: item2.itemName, //1
                    minUnitName: item2.unitName,
                    soldPrice: item2.unitPrice, //1
                    propertiesName: item2.propertiesName,
                    skuCode: item2.skuCode,
                    itemType: '3',
                    id: item2.id, //1
                    itemCode: item2.itemCode, //1
                    batchNumber: item2.batchNumber,
                },
                childVal: false
            })))
        }
        if (item.itemGroupInfoList?.length > 0) {
            mainChildMaterialList.push(...item.itemGroupInfoList.map(item3 => ({
                ...{
                    itemId: item3.itemId,
                    skuId: item3.itemChildId, //1skuid,
                    decimalNum: item3.childInventoryNumber, //1
                    num: 0, //数量,
                    inoutPrice: item3.inoutPriceDec, //价格,
                    sumPrice: item3.childTotalPrice, //总价
                    itemTitle: item3.itemChildName, //1
                    minUnitName: item3.minUnitName,
                    soldPrice: item3.inoutPriceDec, //1
                    propertiesName: item3.propertiesName,
                    skuCode: item3.itemChildCode,
                    itemType: '3',
                    id: item3.id, //1
                    itemCode: item3.itemChildCode, //1
                    batchNumber: item3.batchNumber,
                },
                childVal: true
            })))
        }
    })
    // console.log(mainChildMaterialList);
</script>
</body>
</html>
